package com.RLFZ.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.RLFZ.entity.BanJi;
import com.RLFZ.entity.XueYuan;
import com.RLFZ.service.BanJiService;
import com.RLFZ.service.XueYuanService;
import com.RLFZ.vo.BanJiVo;
import com.RLFZ.vo.DataView;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 服务控制器
 *
 * @author zh
 * @since 2022-08-25 17:16:58
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("/banji")
public class BanJiController  {
    @Autowired
    private BanJiService banJiService;
    @Autowired
    private XueYuanService xueYuanService;

    @RequestMapping("/toBanJi")
    public String toNews(){
        return "banji/banji";
    }
    /**
     * 查询所有角色带有分页，带有条件查询
     */
    @RequestMapping("/listBanJi")
    @ResponseBody
    public DataView listNews(BanJiVo banJiVo){
        QueryWrapper<BanJi> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(banJiVo.getName()),"name",banJiVo.getName());
        IPage<BanJi> ipage=new Page<>(banJiVo.getPage(),banJiVo.getLimit());
        banJiService.page(ipage,queryWrapper);
        return new DataView(ipage.getTotal(),ipage.getRecords());
    }

    @RequestMapping("/addOrUpdateBanJi")
    @ResponseBody
    public DataView addOrUpdateNews(BanJi banJi){
        DataView dataView=new DataView();
        String xueYuanName = banJi.getXueYuanName();
        if (xueYuanName.length()<3){
            banJi.setXueYuanId(Integer.parseInt(xueYuanName));
            QueryWrapper<XueYuan>queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",xueYuanName);
            List<XueYuan> list = xueYuanService.list(queryWrapper);
            String name = list.get(0).getName();
            banJi.setXueYuanName(name);
        }
        boolean save = banJiService.saveOrUpdate(banJi);
        dataView.setMsg("修改或添加成功");
        dataView.setCode(200);
        return dataView;
    }
    @RequestMapping("/deleteByIdBanJi")
    @ResponseBody
    public DataView delBanJiById(Integer id){
        DataView dataView=new DataView();
        boolean save = banJiService.removeById(id);
        if (save==false){
            dataView.setMsg("删除失败");
            dataView.setCode(100);
            return dataView;
        }
        dataView.setMsg("删除成功");
        dataView.setCode(200);
        return dataView;
    }

    /**
     * 批量删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteListBanJi")
    @ResponseBody
    public DataView deleteListBanJi(Integer[] id){
        for (int i=0;i<id.length;i++){
            banJiService.removeById(id[i]);
        }
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除数据成功!");
        return dataView;
    }
}